import Toast from '@vant/weapp/toast/toast';
Page({

    /**
     * 页面的初始数据
     */

    data: {
      //收货详情  
      addressList:[],
      aa:''

    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {

        var currentPages=getCurrentPages();

        var prePages=currentPages[currentPages.length-2];

      var arr = wx.getStorageSync('addressList') || [];

      // 更新数据  

      this.setData({

        addressList: arr

      });
    //   地址数据

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onPullDownRefresh: function () {
        this.onRefresh();
    },

    onShow: function () {
      this.onLoad();
    },

    onUnload: function () {
        var arr = this.data.addressList;
        wx.setStorageSync('addressList', arr);
    },

    change:function(e){/* 选择按钮 */
        var items = this.data.addressList;
        for (var i = 0; i < items.length; i++){
          if (items[i].mobile == this.data.aa){
            for (var j = 0; j < items.length; j++) {
              if (items[j].checked && j != i) {
                  items[j].checked = false;
                }
            }
            items[i].checked = !(items[i].checked);
          }
        }
        this.setData({
          addressList: items
        });
        Toast.success('地址更新成功');
    },

    addAddress:function(){/* 新增地址 */
      wx.navigateTo({ url: '/pages/sy/sy' });
    },

    radioChange: function (e) {
          this.data.aa = e.detail.value;
        },

    /* 删除item */
    delAddress: function (e) {
        var id = e.currentTarget.dataset.id//数组下标
        this.data.addressList.splice(id, 1);
         // 更新data数据对象  
        if (this.data.addressList.length > 0) {
            this.setData({
                addressList: this.data.addressList
            })
            wx.setStorageSync('addressList', this.data.addressList);
        } else {
                this.setData({
                    addressList: this.data.addressList
                })
            wx.setStorageSync('addressList', []);
        }
    },
        // qd点击
        qdAddress:function(){
        
        wx.switchTab({
          url: '/pages/shop/shop',
        })
    },
  })